var $table = $('#table');
var URI_LIST = "/sec/session/list";
var URI_LOGOUT = "/sec/session/forceLogout/";
var idField = "sessionId";
var fieldCloumns = [
    {field: 'ck', checkbox: true},
    {field: 'sid', title: '系统'},
    {field: 'userName', title: '登录名'},
    {field: 'browser', title: '浏览器'},
    {field: 'browserVersion', title: '浏览器版本'},
    {field: 'operatingSystem', title: '操作系统'},
    {field: 'loginTime', title: '登录时间'},
    {field: 'uid', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
]
$(function () {
    $table.bootstrapTable({
        url: URI_LIST,
        idField: idField,
        toolbar: '#toolbar',
        columns: fieldCloumns
    });
});

// 格式化操作按钮
function actionFormatter(value, row, index) {
    return [
        '<a class="delete" href="javascript:;" onclick="deleteAction(' + value + ')" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
    ].join('');
}

// 删除
var deleteDialog;

function deleteAction(value) {
    var rows = getRows(value);
    if (rows.length != 1) {
        showMessage("请选择一条记录！", "error");
    } else {
        deleteDialog = $.confirm({
            type: 'red',
            animationSpeed: 300,
            title: false,
            content: '确认是否强制该用户退出系统？',
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'waves-effect waves-button',
                    action: function () {
                        $.ajax({
                            type: 'post',
                            url: URI_LOGOUT + rows[0].sessionId,
                            success: function (result) {
                                if (result.code != "0000") {
                                    showMessage(result.code || result.desc, "error");
                                } else {
                                    deleteDialog.close();
                                    showMessage("强制退出成功");
                                    $table.bootstrapTable('refresh');
                                }
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    }
}

function getRows(value) {
    var rows = []
    if (value) {
        rows.push($table.bootstrapTable('getRowByUniqueId', value));
    } else {
        rows = $table.bootstrapTable('getSelections');
    }
    return rows;
}